In this example, interact is used to build a UI for exploring San Francisco department purchases by city agency data.
In [ ]:
# Import Pandas and then load the data.
from pandas import read_csv
df = read_csv('SFDeptPurchases.csv')
You can take a quick look at the first 5 rows of the data set using a slice. Pandas knows how to display this as a table in IPython.
In [ ]:
df[:5]
Notice that the totals are of type object (strings) instead of numbers.
In [ ]:
df[:5]['Total']
Remove the dollar sign from the strings and cast them to numbers.
In [ ]:
df['Total'] = df['Total'].str.replace(r'[$,]', '').convert_objects(convert_numeric=True)
In [ ]:
df[:5]['Total']
Now the data can be explored using matplotlib and interact. The following function plots the costs of the selected parameter type.
In [ ]:
%matplotlib inline
from matplotlib import pyplot as plt
from pandas import DataFrame
def plot_by(df, column='Dept Name', count=10, ascending=False):
# Group the data by the column specified and sum the totals.
data = df.groupby(column)['Total'].sum().dropna()
# Sort the data.
data = DataFrame(data, columns=['Total']).sort('Total', ascending=ascending)
# Plot the subset of the sorted data that the user is interested in.
data = data[:count].plot(kind='bar')
# Plot settings.
plt.title('%s Costs' % column)
plt.ylabel('Cost ($)')
In [ ]:
from IPython.html.widgets import interact, fixed
interact(plot_by, df=fixed(df), column=df.columns.tolist(), count=(5,15));